package com.wxh.bomb.httpserver.server;

import com.wxh.bomb.httpserver.utils.CloseUtil;

import java.io.IOException;
import java.net.ServerSocket;

public class Server {
	private ServerSocket server;
	
	public static final String CRLF="\r\n";
	public static final String BLANK=" ";
	
	private boolean shutdown = false;
	
	/**
	 * 启动服务 
	 */
	public void start(){
		try {
			server = new ServerSocket(8888);
			this.receive();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 接受客户端
	 */
	public void receive(){
		try {
			while(!shutdown){
				new Thread(new Dispatcher(server.accept())).start();
			}
		} catch (IOException e) {
			stop();
		}
	}
	
	/**
	 * 停止服务
	 */
	public void stop(){
		shutdown = true;
		CloseUtil.closeSocket(server);
	}

}
